<template>
	<view class="addr-from-box">
		<cu-custom :isBack="true" bgColor="bg-fa">
			<block slot="content">{{title}}</block>
		</cu-custom>
		
		<view class="addr-from">
			<view class="from-item">
				<view class="item-label">
					姓名
				</view>
				<view class="item-content">
					<input type="text" class="inp" v-model="linkName" :maxlength="8" placeholder="请输入姓名" />
				</view>
			</view>
			
			<view class="from-item">
				<view class="item-label">
					电话号码
				</view>
				<view class="item-content">
					<input type="text" class="inp" v-model="linkPhone" :maxlength="11" placeholder="请输入电话号码" />
				</view>
			</view>
			
			<view class="from-item">
				<view class="item-label">
					地区
				</view>
				<view class="item-content">
					<picker @change="addrChange" @columnchange="columnchange" :value="addrIndex" :range="addrList" range-key="name" mode='multiSelector'>
						<view class="city u-line-1">{{addrText}}</view>
					</picker>
				</view>
			</view>
			
			<view class="from-item area-item">
				<view class="item-label">
					详细地址
				</view>
				<view class="item-content">
					<textarea v-model="addr"  placeholder="街道牌  门牌号等" class="area"></textarea>
				</view>
			</view>
			
		</view>
		
		<view class="from-item defa-item">
			<view class="item-label">
				设置为默认地址
			</view>
			<view class="item-content" style="text-align: right;">
				<u-switch v-model="isDefault" active-color="#FA436A" inactive-color="#ffffff" size="40"></u-switch>
			</view>
		</view>
		
		<view class="del-box">
			<text @tap="show = true">删除地址</text>
		</view>
		
		<view class="add-btn-box">
			<view class="add-btn">
				保存
			</view>
		</view>
		
		<u-modal v-model="show" :content="'是否删除地址信息?'" :show-cancel-button="true" @confirm="confirmDel"></u-modal>
		
	</view>
</template>

<script>
	export default{
		name:"addrFrom",
		data(){
			return {
				title:"添加地址信息",
				type:0,
				show:false,
				addrId:null,
				linkName:"",//联系人姓名
				linkPhone:"",//联系人电话
				addrList:[[{id:"none",name:"请选择"},{id:2,name:"广西"},{id:3,name:"广东"}],[],[]],
				addrIndex:null,
				addr:"",
				isDefault:true,
			}
		},
		computed:{
			addrText(){
				if(this.addrIndex == null || this.addrIndex.includes(0)){
					return "选择省/市/区"
				}
				let text = this.addrList[0][this.addrIndex[0]].name + "/" + this.addrList[1][this.addrIndex[1]].name + "/" + this.addrList[2][this.addrIndex[2]].name
				return text
			}
		},
		methods:{
			
			/*  删除地址  */
			confirmDel(){
				this.show = false
			},
			
			
			/*  确认选择地址  */
			addrChange(e){
				let val= e.detail.value
				this.addrIndex = val
			},
			/*  地址栏每一列改变  */
			columnchange(e){
				let column = e.detail.column
				let value = e.detail.column
				if(column !=2 && value !='none'){
					let arr = column == 1 ? [{id:'none',name:"请选择"},{id:4,name:"南宁"},{id:5,name:"桂林"},{id:6,name:"柳州"}] : [{id:'none',name:"请选择"},{id:7,name:"青秀区"},{id:8,name:"江南区"}]
					this.addrList[column + 1] = arr
					console.log(this.addrList)
				}
			},
		},
		onLoad(e) {
			let type = e.type
			let id = e.id
			let len = getCurrentPages().length
			let has = true
			if(!type){
				has = false
			}
			
			if(type == 1 && !id){
				has = false
			}
			
			if(!has){
				if(len == 1){
					uni.switchTab({
						url:"/pages/index/index"
					})
				}else{
					uni.navigateBack({
						delta:1
					})
				}
				
				return false
			}
			
			this.addrId = id
			this.type = type
			let title = type == 0 ?  '添加地址信息' : '编辑地址信息'
			this.title = title
			uni.setNavigationBarTitle({
			    title: title
			});
		}
	}
</script>

<style scoped lang="scss">
	.addr-from-box{
		.addr-from{
			width: calc(100% - 40rpx);
			background: #FFFFFF;
			margin-top: 30rpx;
			margin-left: 20rpx;
			padding: 20rpx;
			border-radius: 10rpx 10rpx;
			.area-item{
				height: 200rpx;
				align-items: flex-start;
				margin-top: 30rpx;
			}
			
		}
		.from-item{
			width: 100%;
			height: 90rpx;
			border-bottom: 2rpx solid #f1f1f1;
			margin-bottom: 10rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.item-label{
				width: 140rpx;
				font-size: 28rpx;
				color: #333;
			}
			.item-content{
				flex-grow: 1;
			}
			.inp{
				font-size: 28rpx;
				color: #666666;
				padding-left: 10rpx;
			}
			.city{
				color: #666666;
				font-size: 28rpx;
			}
			.area{
				width: 100%;
				height: 200rpx;
				color: #666666;
				font-size: 28rpx;
				padding: 0 20rpx;
			}
		}
		.defa-item{
			width: calc(100% - 40rpx);
			margin-left: 20rpx;
			background: #FFFFFF;
			border-radius: 10rpx 10rpx;
			margin-top: 40rpx;
			padding: 0 20rpx;
			.item-label{
				width: 300rpx;
				font-size: 28rpx;
				color: #333;
			}
		}
		.del-box{
			color: #FA3534;
			margin-top: 40rpx;
			padding: 0 40rpx;
		}
		.add-btn-box{
			width: 100%;
			height: 120rpx;
			margin-top: 60rpx;
			.add-btn{
				width: 90%;
				margin-left: 5%;
				height: 92rpx;
				border-radius: 92rpx 92rpx;
				color: #FFFFFF;
				font-size: 32rpx;
				text-align: center;
				line-height: 92rpx;
				background: #FA436A;
			}
		}
	}
	
</style>
